import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue'
import laravel from 'laravel-vite-plugin';
import autoImport from "unplugin-auto-import/vite";
import Components from 'unplugin-vue-components/vite';
import autoImportPlugin from '@opentiny/unplugin-tiny-vue'
// import importPlugin from '@opentiny/vue-vite-import'
import path from "path"
const resolve = (name:string = "")=>{
    return path.resolve(__dirname, `./resources${name}`)
}

export default defineConfig(({ mode, command })=>{
    const isBuild = command === "build"
    const env = loadEnv(mode, process.cwd());
    return {
        resolve: {
            alias:[
                { find: /~\//, replacement: resolve("")+"/" },
                { find: /@\//, replacement: resolve("/src")+"/" },
                // { find: /\@opentiny\/vue-theme\/(?!(smb|theme))/, replacement: '@opentiny/vue-theme/smb-theme/' },
            ]
        },
        define: {
            "process.env": {
                mode: mode,
                command: command,
                ...process.env
            },
        },
        plugins: [
            vue(),
            laravel({
                input: [resolve("/css/app.css"), resolve("/src/main.ts")],
                refresh: true,
            }),
            autoImportPlugin(),
            autoImport({
                imports: ["vue", "vue-router", "pinia"],
                dts: resolve("/src/types/auto-imports.d.ts"),
                dirs: ["./resources/src/hooks/**","./resources/src/store/**"],
            }),
            Components({
                dts: resolve("/src/types/components.d.ts"),
                types: [{ from: 'vue-router', names: ['RouterLink', 'RouterView'] }],
            }),
        ],
        build: {
            target: 'es2015',
            polyfillDynamicImport: false,
            reportCompressedSize: false,
            sourcemap: false,
            commonjsOptions: {
                ignoreTryCatch: false,
                transformMixedEsModules: true,
            }
        },
        esbuild: {
            drop: isBuild ? ['console', 'debugger'] : [],
        }
    }
});
